package net.lainiao.morethread.Chapter3.demo1;

import java.util.concurrent.Semaphore;

/**
 * Created by Administrator on 2017/3/1.
 */
public class PrintQueue {
    private final Semaphore semaphore;

    public PrintQueue() {
        semaphore = new Semaphore(1);
    }

    public void printJob(Object document) {
        try {
            semaphore.acquire();
            long duration=(long)(Math.random()*10);
            System.out.printf("%s PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),duration);
            Thread.sleep(duration);
            System.out.println("Print Job is Over for Thread:"+Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semaphore.release();
        }
    }



}
